package com.bicon.botu.mall.order.api;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.mall.order.dto.OrderChildDto;
import com.bicon.botu.mall.order.dto.OrderDetailDto;
import com.bicon.botu.mall.order.param.OrderDetailParam;
import com.bicon.botu.mall.order.service.IOrderChildService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Type;
import java.util.List;

/**
 * <p>
 * 子订单前端控制器
 * </p>
 *
 * @author 李双喜
 * @since 2018-05-14
 */
@RestController
@RequestMapping("/V3.0.0/api/order-child")
public class OrderChildApiV3 extends BaseController {
    @Autowired
    private IOrderChildService iOrderChildService;

    /**
     * 购物车跳转提交订单接口
     *
     * @param jsonString json字符串
     * @return
     */
    @RequestMapping("/affirm-order")
    public BaseResult affirmOrder(@RequestParam("jsonString") String jsonString, @RequestParam("userId") String userId) {
        if (StringUtils.isEmpty(jsonString)) {
            return new BaseResult(400, "参数错误");
        }
        OrderChildDto result = iOrderChildService.affirmOrder(jsonString, userId);
        return sendResult200(result);
    }


    /**
     * 计算订单商品的运费
     *
     * @param jsonString json字符串
     * @return
     */
    @RequestMapping("/freight-calculate")
    public BaseResult freightOrder(@RequestParam String jsonString) {
        if (StringUtils.isEmpty(jsonString)) {
            return sendResult400();
        }
        Type type = new TypeReference<List<OrderDetailParam>>() {
        }.getType();
        //分组订单
        List<OrderDetailParam> paramList = JSONObject.parseObject(jsonString, type);
        OrderChildDto result = new OrderChildDto();
        if(paramList != null && !paramList.isEmpty()) {
           result = iOrderChildService.freightCalculate(paramList);
        }

        return sendResult200(result);
    }

    /**
     * 提交订单接口
     *
     * @param jsonString json字符串
     * @return
     */
    @RequestMapping("/commit-order")
    public BaseResult commitOrder(@RequestParam String jsonString) {
        if (StringUtils.isEmpty(jsonString)) {
            return new BaseResult(400, "参数错误");
        }
        return iOrderChildService.commitOrder(jsonString);
    }

    /**
     * 根据总订单id物理删除订单
     *
     * @param id 总订单id
     * @return
     */
    @PostMapping("/delete-order")
    public BaseResult deleteMaxOrder(@RequestParam String id) {
        if (StringUtils.isEmpty(id)) {
            return new BaseResult(400, "参数错误");

        }
        Boolean result = iOrderChildService.deleteMaxOrder(id);
        if (result) {
            return new BaseResult(200, "success", result);
        }
        return new BaseResult(500, "error", result);
    }

    /**
     * 查询订单详情接口
     *
     * @param orderGoodsId 分组订单id
     * @return
     */
    @PostMapping("/select-detail")
    public BaseResult selectDetail(@RequestParam String orderGoodsId) {
        if (StringUtils.isEmpty(orderGoodsId)) {
            return new BaseResult(400, "参数错误");
        }
        OrderDetailDto orderDetailDto = iOrderChildService.selectDetail(orderGoodsId);
        return new BaseResult(200, "success", orderDetailDto);
    }

    /**
     * 订单支付接口
     *
     * @param userId       转账人
     * @param maxOrderId   总订单id
     * @param channel      支付类型
     * @param storeId     店铺id
     * @param password     密码
     * @return
     */
    @PostMapping("/pay")
    public BaseResult pay(@RequestParam("userId") String userId, @RequestParam("maxOrderId") String maxOrderId,
                          @RequestParam("channel") String channel, @RequestParam("storeId") String storeId,
                          @RequestParam(value = "password",required = false) String password) {
        if (StringUtils.isAnyBlank(userId, maxOrderId, channel, storeId)) {
            return new BaseResult(400, "参数错误");
        }
        return iOrderChildService.pay(userId, maxOrderId, channel, storeId, password);
    }



}
